from maya import OpenMaya as OM , cmds

def ray( mesh , point ):
    sList = OM.MSelectionList()
    sList.add( mesh )
    item = OM.MDagPath()
    sList.getDagPath(0, item)
    item.extendToShape()
    fnMesh = OM.MFnMesh(item)    
    
    raySource = OM.MFloatPoint( point[0], point[1], point[2], 1.0 )
    rayDir = OM.MFloatVector( 0.0, -1.0, 0.0 )
    
    _point = OM.MFloatPoint( 0.0, 0.0, 0.0 )
    
    _script = OM.MScriptUtil()
    _script.createFromInt( 0 )
    _facePtr = _script.asIntPtr()
    
    #hit = fnMesh.allIntersections(raySource, rayDir, faceIds, triIds, idsSorted,\
                #worldSpace, maxParam, testBothDirections,\
                #accelParams, sortHits, hitPoints, hitRayParams,\
                #hitFaces, hitTris, hitBarys1, hitBarys2, tolerance)  
                
    hit  = fnMesh.anyIntersection(raySource,rayDir,None,None,False, \
                OM.MSpace.kWorld, 999999, False,\
                None, _point, None,\
                _facePtr, None, None, None, 0.0001)

    #hit = fnMesh.closestIntersection(raySource, rayDir, faceIds, triIds, idsSorted,\
                #worldSpace, maxParam, testBothDirections,\
                #accelParams, hitPoints, hitRayParams,\
                #hitFaces, hitTris, hitBarys1, hitBarys2, tolerance)

    #ff = hitPoints[0]
    ff = _point

    if hit == False:
        return -1000, OM.MVector(0,1,0)
        
    #vec = [ float(x) for x in cmds.polyInfo( mesh +'.f['+ str( hitFaces[0] ) +']',fn=1)[0].split(':')[1].split('\n')[0].split(' ')[1:] ]    
    vec = [ float(x) for x in cmds.polyInfo( mesh +'.f['+ str( _script.getInt(_facePtr)) +']',fn=1 )[0].split(':')[1].split('\n')[0].split(' ')[1:] ]
    return ff.y, OM.MVector(vec[0] , vec[1] , vec[2])